package com.itheima.youdianda.showBuDing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddArticle;
import com.itheima.youdianda.domain.YddCate;
import com.itheima.youdianda.domain.YddUserData;
import com.itheima.youdianda.dto.ArticleDto;
import com.itheima.youdianda.mapper.YddArticleMapper;
import com.itheima.youdianda.showBuDing.dto.ShowDto;

import com.itheima.youdianda.showBuDing.mapper.MyYddArticleMapper;
import com.itheima.youdianda.showBuDing.service.MyYddArticleService;
import com.itheima.youdianda.service.YddCateService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyYddArticleServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/2/15 0:41
 * @Version 1.0
 */
@Service
public class MyYddArticleServiceImpl extends ServiceImpl<MyYddArticleMapper,YddArticle> implements MyYddArticleService {

    @Autowired
    private MyYddArticleMapper myYddArticleMapper;
    @Autowired
    private YddCateService yddCateService;

    @Override
    public Map show(Integer id) {
        //返回结果集和
        Map<String,Object> map = new HashMap<>();
        //获取前一篇文章数据
        YddArticle prev = myYddArticleMapper.prev(id);
        List<Map> prevList = new ArrayList<>();
        Map<String,Object> prevMap = new HashMap<>();
        if (prev != null) {
            prevMap.put("id",prev.getId());
            prevMap.put("title",prev.getTitle());
            prevList.add(prevMap);

        }
        map.put("prev",prevList);

        //获取下一篇文章的数据
        YddArticle next = myYddArticleMapper.next(id);
        List<Map> nextList = new ArrayList<>();
        Map<String,Object> nextMap = new HashMap<>();
        if (next != null) {
            nextMap.put("id",next.getId());
            nextMap.put("title",next.getTitle());
            nextList.add(nextMap);

        }
        map.put("next",nextList);

        //获取当前文章详情
        YddArticle yddArticle = myYddArticleMapper.selectById(id);

        ShowDto showDto = new ShowDto();
        BeanUtils.copyProperties(yddArticle,showDto);
        showDto.setCreate_date(yddArticle.getCreateDate());
        showDto.setUpdate_date(yddArticle.getUpdateDate());
        showDto.setUser_id(yddArticle.getUserId());

        YddCate cate = yddCateService.getById(yddArticle.getCateid());
        if (cate != null) {
            showDto.setCatename(cate.getCatename());
        }

        showDto.setLike(false);
        showDto.setCollect(false);

        map.put("info",showDto);

        //查询推荐文章
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddArticle::getIshot,1).eq(YddArticle::getIstop,1).last("limit 12");
        List<YddArticle> list = myYddArticleMapper.selectList(lqw);
        map.put("recommend",list);

        return map;
    }
}
